package c.j.a.d.b.h;

import android.util.SparseArray;
import c.j.a.c.y.a.i;
import c.j.a.d.b.b.g;
import c.j.a.d.b.d.a0;
import c.j.a.d.b.d.b;
import c.j.a.d.b.d.b0;
import c.j.a.d.b.d.d;
import c.j.a.d.b.d.f0;
import c.j.a.d.b.d.h0;
import c.j.a.d.b.d.j;
import c.j.a.d.b.d.l0;
import c.j.a.d.b.d.m0;
import c.j.a.d.b.d.q;
import c.j.a.d.b.e.k;
import c.j.a.d.b.e.l;
import c.j.a.d.b.e.s;
import c.j.a.d.b.g.a;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

/* compiled from: DownloadTask */
public class c {
    public com.ss.android.socialbase.downloader.g.c a;
    public k b;
    public l c;
    public final Map<g, b> d = new ConcurrentHashMap();
    public final SparseArray<g> e = new SparseArray();
    public final SparseArray<b> f = new SparseArray();
    public final SparseArray<b> g = new SparseArray();
    public final SparseArray<b> h = new SparseArray();
    public d i;
    public m0 j;
    public b0 k;
    public q l;
    public com.ss.android.socialbase.downloader.g.c.b m = new com.ss.android.socialbase.downloader.g.c.b();
    public l0 n;
    public f0 o;
    public s p;
    public j q;
    public boolean r = false;
    public h0 s;
    public a0 t;

    public SparseArray<b> a(g gVar) {
        if (gVar == g.MAIN) {
            return this.f;
        }
        if (gVar == g.SUB) {
            return this.g;
        }
        return gVar == g.NOTIFICATION ? this.h : null;
    }

    public c b(int i, b bVar) {
        if (bVar != null) {
            synchronized (this.f) {
                this.f.put(i, bVar);
            }
            Map map = this.d;
            g gVar = g.MAIN;
            map.put(gVar, bVar);
            synchronized (this.e) {
                this.e.put(i, gVar);
            }
        }
        return this;
    }

    public void c(int i, b bVar, g gVar, boolean z) {
        SparseArray a = a(gVar);
        if (a == null) {
            if (z && this.d.containsKey(gVar)) {
                this.d.remove(gVar);
            }
            return;
        }
        synchronized (a) {
            if (z) {
                Object bVar2;
                if (this.d.containsKey(gVar)) {
                    bVar2 = (b) this.d.get(gVar);
                    this.d.remove(gVar);
                }
                if (bVar2 != null) {
                    i = a.indexOfValue(bVar2);
                    if (i >= 0 && i < a.size()) {
                        a.removeAt(i);
                    }
                }
            } else {
                a.remove(i);
                synchronized (this.e) {
                    gVar = (g) this.e.get(i);
                    if (gVar != null && this.d.containsKey(gVar)) {
                        this.d.remove(gVar);
                        this.e.remove(i);
                    }
                }
            }
        }
    }

    public final void d(SparseArray<b> sparseArray, SparseArray<b> sparseArray2) {
        sparseArray.clear();
        for (int i = 0; i < sparseArray2.size(); i++) {
            int keyAt = sparseArray2.keyAt(i);
            b bVar = (b) sparseArray2.get(keyAt);
            if (bVar != null) {
                sparseArray.put(keyAt, bVar);
            }
        }
    }

    public void e(c cVar) {
        this.b = cVar.b;
        this.c = cVar.c;
        this.d.clear();
        this.d.putAll(cVar.d);
        synchronized (this.f) {
            this.f.clear();
            h(cVar.f, this.f);
        }
        synchronized (this.g) {
            this.g.clear();
            h(cVar.g, this.g);
        }
        synchronized (this.h) {
            this.h.clear();
            h(cVar.h, this.h);
        }
        this.i = cVar.i;
        this.j = cVar.j;
        this.k = cVar.k;
        this.l = cVar.l;
        this.n = cVar.n;
        this.o = cVar.o;
        this.p = cVar.p;
        this.q = cVar.q;
        this.s = cVar.s;
        this.t = cVar.t;
    }

    public c f(int i, b bVar) {
        if (bVar != null) {
            synchronized (this.g) {
                this.g.put(i, bVar);
            }
            Map map = this.d;
            g gVar = g.SUB;
            map.put(gVar, bVar);
            synchronized (this.e) {
                this.e.put(i, gVar);
            }
        }
        return this;
    }

    public void g(int i, b bVar, g gVar, boolean z) {
        if (bVar != null) {
            if (z) {
                Map map = this.d;
                if (map != null) {
                    map.put(gVar, bVar);
                    synchronized (this.e) {
                        this.e.put(i, gVar);
                    }
                }
            }
            SparseArray a = a(gVar);
            if (a != null) {
                synchronized (a) {
                    a.put(i, bVar);
                }
            }
        }
    }

    public final void h(SparseArray sparseArray, SparseArray sparseArray2) {
        if (sparseArray != null && sparseArray2 != null) {
            int size = sparseArray.size();
            for (int i = 0; i < size; i++) {
                int keyAt = sparseArray.keyAt(i);
                sparseArray2.put(keyAt, sparseArray.get(keyAt));
            }
        }
    }

    public void i(c cVar) {
        for (Entry entry : cVar.d.entrySet()) {
            if (entry != null) {
                if (!this.d.containsKey(entry.getKey())) {
                    this.d.put(entry.getKey(), entry.getValue());
                }
            }
        }
        try {
            if (cVar.f.size() != 0) {
                synchronized (this.f) {
                    k(this.f, cVar.f);
                    h(cVar.f, this.f);
                }
            }
            if (cVar.g.size() != 0) {
                synchronized (this.g) {
                    k(this.g, cVar.g);
                    h(cVar.g, this.g);
                }
            }
            if (cVar.h.size() != 0) {
                synchronized (this.h) {
                    k(this.h, cVar.h);
                    h(cVar.h, this.h);
                }
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public c j(int i, b bVar) {
        if (bVar != null) {
            synchronized (this.h) {
                this.h.put(i, bVar);
            }
            Map map = this.d;
            g gVar = g.NOTIFICATION;
            map.put(gVar, bVar);
            synchronized (this.e) {
                this.e.put(i, gVar);
            }
        }
        return this;
    }

    public final void k(SparseArray sparseArray, SparseArray sparseArray2) {
        if (sparseArray != null && sparseArray2 != null) {
            int size = sparseArray2.size();
            for (int i = 0; i < size; i++) {
                sparseArray.remove(sparseArray2.keyAt(i));
            }
        }
    }

    public final void l(g gVar) {
        SparseArray a = a(gVar);
        synchronized (a) {
            for (int i = 0; i < a.size(); i++) {
                b bVar = (b) a.get(a.keyAt(i));
                if (bVar != null) {
                    c.j.a.d.b.e.d.b().f(m(), bVar, gVar, false);
                }
            }
        }
    }

    public int m() {
        com.ss.android.socialbase.downloader.g.c cVar = this.a;
        if (cVar == null) {
            return 0;
        }
        return cVar.g0();
    }

    public void n() {
        a.d("DownloadTask", "same task just tryDownloading, so add listener in last task instead of tryDownload");
        l(g.MAIN);
        l(g.SUB);
        i.n(this.l, this.a, new com.ss.android.socialbase.downloader.e.a(1003, "has another same task, add Listener to old task"), 0);
    }
}
